package com.nbpt.edu.commercesites.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

public class PasswordUtils {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder(12);

    public static String encode(CharSequence rawPassword) {
        if(rawPassword == null) {
            throw new IllegalArgumentException("原始密码不能为null");
        }
        String encoded = ENCODER.encode(rawPassword);
        if(encoded == null || encoded.isEmpty()) {
            throw new IllegalStateException("密码加密失败");
        }
        return ENCODER.encode(rawPassword);
    }

    public static boolean matches(CharSequence rawPassword, String encodedPassword) {
        if(rawPassword == null || encodedPassword == null) {
            return false;
        }
        return ENCODER.matches(rawPassword, encodedPassword);
    }
}
